package com.google.code.tomoponzoo.lab;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;

public class WebView1Activity extends Activity {
	private Button loadButton;
	private Button clearButton;
	private Button lineButton;
	private WebView webView;
	private boolean line = true;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        
        loadButton = (Button)findViewById(R.id.loadButton);
        clearButton = (Button)findViewById(R.id.clearButton);
        lineButton = (Button)findViewById(R.id.lineButton);
        
        // Configure Button (Load).
        loadButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.v("onClick", "Load");
				webView.loadUrl("http://118.151.159.201/dev_aws/node/41");
			}
		});
        
        // Configure Button (Clear)
        clearButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.v("onClick", "Clear");
				webView.clearCache(true);
			}
		});
        
        // Configure Button (Line)
        lineButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.v("onClick", "Line");
				int airplane = line ? 1 : 0;
				Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, airplane);
				
				Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
				intent.putExtra("state", airplane);
				sendBroadcast(intent);
				
				lineButton.setText(line ? R.string.online : R.string.offline);
				
				line = !line;
			}
		});
        
        // Configure WebView.
        LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout0);
        webView = new WebView(this);
        webView.setWebViewClient(new WebViewClientEx());
        webView.setVerticalScrollBarEnabled(false);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSavePassword(false);
        settings.setSaveFormData(false);
        settings.setSupportZoom(false);
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        layout.addView(webView);
    }
    
    private final class WebViewClientEx extends WebViewClient {
    	@Override
    	public void onPageStarted(WebView view, String url, Bitmap favicon) {
    		// TODO Auto-generated method stub
    		Log.v("onPageStarted", url);
    		super.onPageStarted(view, url, favicon);
    	}
    	
    	@Override
    	public void onPageFinished(WebView view, String url) {
    		// TODO Auto-generated method stub
    		Log.v("onPageFinished", url);
    		super.onPageFinished(view, url);
    	}
    	
    	@Override
    	public void onLoadResource(WebView view, String url) {
    		// TODO Auto-generated method stub
    		Log.v("onLoadResource", url);    		
    		super.onLoadResource(view, url);
    	}
    	
    	@Override
    	public void onReceivedError(WebView view, int errorCode,
    			String description, String failingUrl) {
    		// TODO Auto-generated method stub
    		Log.v("onReceivedError", String.valueOf(errorCode));
    		Log.v("onReceivedError", description);
    		Log.v("onReceivedError", failingUrl);
    		
    		super.onReceivedError(view, errorCode, description, failingUrl);
    	}
    	
    	@Override
    	public boolean shouldOverrideUrlLoading(WebView view, String url) {
    		// TODO Auto-generated method stub
    		boolean override = true;
    		return override;
    	}
    }
}